using System;
using System.Collections.Generic;
using TTXNA.src.Engine.Controls;
using TTXNA.src.Engine.GUI.Windows;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using TTXNA.src.Util;
using TTXNA.src.Engine.Input;

namespace TTXNA.src.Engine.GUI
{
    public class GUIManager : TTUpdateable
    {
        private List<Control> _controls;
        private List<Window> gameWindows;
        private List<Window> closeSoon;
        private List<Window> openingSoon;

        private string _searchName;
        private bool _panelDragging;
        
        private int _activeEditBox;
        protected bool _hasFocus;

        public delegate void ControlDelegate(int controlID, object data);


        public GUIManager()
        {
            _controls = new List<Control>();
            gameWindows = new List<Window>();
            closeSoon = new List<Window>();
            openingSoon = new List<Window>();
            _activeEditBox = -1;
        }

        public void OpenNewWindow(Window window)
        {
            openingSoon.Add(window);
        }

        public void CloseWindow(Window window)
        {
            closeSoon.Add(window);
        }

        private void OpenWaitingWindows()
        {
            foreach (Window window in openingSoon)
            {
                gameWindows.Add(window);
            }
            openingSoon.Clear();
        }

        private void CloseClosingWindows()
        {
            if (closeSoon.Count != 0)
            {

                foreach (Window window in closeSoon)
                {
                    gameWindows.Remove(window);
                }
                closeSoon.Clear();
                GC.Collect(); //?
            }
            
        }

        public bool IsOpen(Window window)
        {
            return gameWindows.Contains(window);
        }

        public bool KeyBoardHandler(List<Keys> pressedKeys, char pressedChar, int pressedKey)
        {
            // Go front to back 
            for (int i = _controls.Count - 1; i >= 0; i--)
            {
                if (_controls[i].Disabled)
                {
                    // Ignore disabled Controls 
                    continue;
                }
                if (_controls[i].KeyboardHandler(pressedKeys, pressedChar, pressedKey))
                {
                    return true;
                }

            }
            return false;
        }

        public bool MouseHandler(MouseData mouse)
        {
            bool result = false;

            for (int i = gameWindows.Count - 1; i >= 0; i--)
            {
                Window window = gameWindows[i];

                result = window.MouseHandler(mouse);

                if (result && mouse.LeftMouseButtonDown)
                {
                    gameWindows.Remove(window);
                    gameWindows.Add(window);
                    break;
                }

            }

            return result;
        }

        public void Update(GameTime gameTime)
        {

        }

        public void Render()
        {
            CloseClosingWindows();
            OpenWaitingWindows();

            //Render not always on top
            foreach (Window window in gameWindows)
            {
                if (!window.AlwaysOnTop)
                {
                    window.Update();
                    //if(!(window is VehicleWindow))
                    window.Render();
                }
            }

            foreach (Window window in gameWindows)
            {
                if (window.AlwaysOnTop)
                {
                    window.Update();
                    window.Render();
                }
            }
        }
    }
}
